package com.acme.bankapp.service.bank.collection;

import com.acme.bankapp.domain.bank.client.Client;

/**
 * This class represents primitive collection of {@link com.acme.bankapp.domain.bank.client.Client} objetcs
 *
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class ClientsCollection {
    private final int capacityMultiplier = 2;
    private int size;
    private int capacity;
    private Client[] clients;

    public ClientsCollection(final int initialClientsCount) {
        assert (initialClientsCount > 0);
        if (initialClientsCount <= 0) {
            throw new IllegalArgumentException(String.format("Invalid argument: initialClientsCount: %d", initialClientsCount));
        }
        clients = new Client[initialClientsCount];
        size = 0;
        capacity = initialClientsCount;
    }

    /**
     * @return array of {@link com.acme.bankapp.domain.bank.client.Client} objects
     */
    public Client[] getClients() {
        if (size == 0) {
            return null;
        } else {
            Client[] retClients = new Client[size];
            System.arraycopy(clients, 0, retClients, 0, size);
            return retClients;
        }
    }

    /**
     * This method insert client to collection
     *
     * @param client - client to insert
     */
    public void insertClient(Client client) {
        if (size == capacity) {
            Client[] newClients = new Client[capacity * capacityMultiplier];
            System.arraycopy(clients, 0, newClients, 0, capacity);
            capacity *= capacityMultiplier;
            clients = newClients;
        }
        clients[size++] = client;
    }
}
